package com.rxh.service.impl;


import com.rxh.mapper.merchant.MerchantInfoMapper;
import com.rxh.mapper.merchant.MerchantRatePercentMapper;
import com.rxh.mapper.merchant.MerchantWebSiteMapper;
import com.rxh.mapper.sys.SysConstantMapper;
import com.rxh.pojo.merchant.MerchantInfo;
import com.rxh.pojo.merchant.MerchantRatePercent;
import com.rxh.pojo.merchant.MerchantWebSite;
import com.rxh.pojo.sys.SysConstant;
import com.rxh.service.ChannelRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChannelRateServiceImpl implements ChannelRateService{

	@Autowired
	private MerchantRatePercentMapper merchantRatePercentMapper;
	
	@Autowired
	private SysConstantMapper constantMapper;
	
	@Autowired
    private MerchantInfoMapper merchantInfoMapper;
    
	@Autowired
    private MerchantWebSiteMapper merchantWebSiteMapper;
	

	@Caching(evict = {
			@CacheEvict(value = "merchant_rate_percent", allEntries = true)
	})
	@Override
	public Map<String, Object> save(MerchantRatePercent entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        entity.setCreateTime(timestamp);
        entity.setModifyTime(null);
        
        MerchantRatePercent channelRateObj = new MerchantRatePercent();
        MerchantRatePercent channelRateParam = new MerchantRatePercent();
        
        channelRateParam.setType(entity.getType());
        channelRateParam.setTypeValue(entity.getTypeValue());
        channelRateParam.setElementValue(entity.getElementValue());
        
        channelRateObj =  merchantRatePercentMapper.selectByParam(channelRateParam);
        int affectCount = 0 ;
        String msg = "";
        
        if(null == channelRateObj){
        	
        	
        	affectCount = merchantRatePercentMapper.insertSelective(entity);
        	   
        	
        }else {
        	
        	   msg = "此汇率已经存在，不能重复添加!";
        	
        }
        
		if(affectCount > 0){
			
			map.put("success", 1);
			
		}else{
			
			map.put("success", 0);
			map.put("msg",msg);
		}
		return map ;
		
	}

	@Caching(evict = {
			@CacheEvict(value = "merchant_rate_percent", allEntries = true)
	})
	@Override
	public int deleteById(Long id) {
		
		return merchantRatePercentMapper.deleteByPrimaryKey(id);
	}

	@Caching(evict = {
			@CacheEvict(value = "merchant_rate_percent", allEntries = true)
	})
	@Override
	public int deleteByParam(Map<String, Object> param) {
		
		return merchantRatePercentMapper.deleteByParam(param);
		
	}

	@Caching(evict = {
			@CacheEvict(value = "merchant_rate_percent", allEntries = true)
	})
	@Override
	public Map<String, Object> update(MerchantRatePercent entity) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	Map<String, Object> map = new HashMap<String, Object>();
    	
    	Date createTime=entity.getCreateTime();
    	Date date=new Date();
    	String dataTime=sdf.format(createTime);
    	String currentTime=sdf.format(date);
    	
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        
        String msg = "";

        int affectCount = 0;
        
        if(dataTime.equals(currentTime)){
        	
            entity.setModifyTime(timestamp);
        	affectCount = merchantRatePercentMapper.updateByPrimaryKeySelective(entity);
        	
        	
        }else{
        	
        	entity.setCreateTime(null);
        	entity.setModifyTime(timestamp);
            entity.setStatus(1);
            affectCount = merchantRatePercentMapper.updateByPrimaryKeySelective(entity);
            if(affectCount > 0){
            	
            	entity.setCreateTime(date);
            	entity.setStatus(0);
            	
            	entity.setCreator(entity.getModifier());
            	entity.setModifier(null);
            	entity.setModifyTime(null);
            	entity.setId(null);
            	
            	//查询此天的汇率是否存在
            	MerchantRatePercent channelRateObj =  merchantRatePercentMapper.selectByParam(entity);

//            	if(null == channelRateObj){   //不明
//
//                	entity.setId(UUID.createKey("merchant_rate_percent"));
//                	affectCount = merchantRatePercentMapper.insertSelective(entity);
//
//
            	}else{
            		
            		
            		msg = "此汇率已经存在，不能重复添加!";
            		affectCount = 0;
            		
            	}
            	
            }
            
           
//
//        }
		 
		if(affectCount > 0){
			
			map.put("success", 1);
			
		}else{
			
			map.put("success", 0);
			map.put("msg", msg);
			
		}
		return map ;
		
	}

	@Caching(evict = {
			@CacheEvict(value = "merchant_rate_percent", allEntries = true)
	})
	@Override
	public int updateByIdSelective(MerchantRatePercent entity) {
		return merchantRatePercentMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public MerchantRatePercent selectById(Long id) {
		return merchantRatePercentMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<MerchantRatePercent> queryList(Map<String, Object> param) {
		return merchantRatePercentMapper.queryList(param);
	}

	@Override
	public Map<String, Object> getChannelRateInfo(Map<String, Object> paramMap) {

		Map<String, Object> resultMap = new HashMap<String,Object>();
		Map<String,Object> searchInfoMap  = (Map<String,Object>) paramMap.get("searchInfo");
		if(null != searchInfoMap){
			
			paramMap.put("type", searchInfoMap.get("type"));
			paramMap.put("typeValue", searchInfoMap.get("typeValue"));
			paramMap.put("elementValue", searchInfoMap.get("elementValue"));
			paramMap.put("creator", searchInfoMap.get("creator"));
			paramMap.put("modifier", searchInfoMap.get("modifier"));
			paramMap.put("minCreateTime", searchInfoMap.get("minCreateTime"));
			paramMap.put("maxCreateTime", searchInfoMap.get("maxCreateTime"));
			paramMap.put("minModifyTime", searchInfoMap.get("minModifyTime"));
			paramMap.put("maxModifyTime", searchInfoMap.get("maxModifyTime"));
			
		}
		
		paramMap.put("sortFieldName",paramMap.get("sortFieldName"));
		paramMap.put("sortType", paramMap.get("sortType"));

		if(null != paramMap.get("pageFirst") && null !=paramMap.get("pageSize")){

			Integer pageFirst = Integer.parseInt(paramMap.get("pageFirst").toString());
			Integer pageSize =  Integer.parseInt(paramMap.get("pageSize").toString());
			paramMap.put("pageFirst", pageFirst);
			paramMap.put("pageSize", pageSize);

		}

		List<MerchantRatePercent> channelRateList = merchantRatePercentMapper.queryList(paramMap);
		int resultTotal = merchantRatePercentMapper.getMerchantRatePercentAllResultCount(paramMap);
		resultMap.put("resultTotal", resultTotal);
		for(MerchantRatePercent merchantRatePercent : channelRateList){
			
			Short type =  merchantRatePercent.getType();
			
			   if(type == 0){
				   
				   merchantRatePercent.setTypeValueName("通用");
				   
			   }else if(type == 1){
				
				SysConstant constant = new SysConstant();
				constant.setGroupCode("Currency");
				constant.setFirstValue(merchantRatePercent.getTypeValue());
				constant = constantMapper.selectByConstant(constant);
				merchantRatePercent.setTypeValueName(constant.getName());
				
			}else if(type == 2){
				
			    MerchantWebSite merchantWebSite = merchantWebSiteMapper.selectByPrimaryKey(Integer.valueOf(merchantRatePercent.getTypeValue()));
				merchantRatePercent.setTypeValueName(merchantWebSite.getSiteUrl());
				
			}else{
				
				merchantRatePercent.setTypeValueName(merchantRatePercent.getTypeValue());
				
			}

			
			
		}
		resultMap.put("channelRateList", channelRateList);
		
		
		return resultMap;
		
	}

	@Override
	public Map<String, Object> getChannelRateInit(Integer type) {
		
		   Map<String, Object> resultMap = new HashMap<String,Object>();
		   List<MerchantInfo> merchants = null;
		   List<SysConstant> selectCurrencys = null;
		   List<MerchantWebSite> websites = null;
		   
           if(type == 1){
			   
			   selectCurrencys = constantMapper.selectByGroupNameAndSortValueIsNotNULL("Currency");
			   
		   }else if(type == 2){
			   
			   websites = merchantWebSiteMapper.selectAllIdAndMerIdAndWeb();
			   
		   }else if(type == 3 ){
			   
			   merchants = merchantInfoMapper.selectAllIdAndName();

			   
		   }else if(type == 4){
			   
			   merchants = merchantInfoMapper.selectAllIdAndName();
			   selectCurrencys = constantMapper.selectByGroupName("Currency");
			   websites = merchantWebSiteMapper.selectAllIdAndMerIdAndWeb();
			   
		   }
			
			resultMap.put("merchants", merchants);
			resultMap.put("selectCurrencys", selectCurrencys);
			resultMap.put("websites",websites);
			
		    return resultMap;
		    
		
	}

	@Caching(evict = {
			@CacheEvict(value = "merchant_rate_percent", allEntries = true)
	})
	@Override
	public Map<String, Object> deleteByParams(Map<String, Object> paramMap) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		int result = merchantRatePercentMapper.deleteByParam(paramMap);
		if(result >= 1){
			
			map.put("result", "1");
			
		}else{
			
			map.put("result", "0");
		}
		
		return map;
	}

	@Override
	public Map<String, Object> getSitesByParamInit(Map<String, Object> paramMap) {
		
		   Map<String, Object> resultMap = new HashMap<String,Object>();
		   List<MerchantWebSite> merWebsites = merchantWebSiteMapper.selectAllByParam(paramMap);
		   resultMap.put("merWebsites",merWebsites);
			
		   return resultMap;
		   
	}


	@Caching(evict = {
			@CacheEvict(value = "merchant_rate_percent", allEntries = true)
	})
	@Override
	public Map<String, Object> deleteByIds(Map<String, Object> paramMap) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		String id = paramMap.get("id") + "";
		
		int result = merchantRatePercentMapper.deleteByIds(id);
		
		if(result >= 1){
			
			map.put("result", "1");
			
		}else{
			
			map.put("result", "0");
		}
		
		return map;
	}

	@Cacheable(value = "merchant_rate_percent", key = "'merchant_rate_percent'")
	@Override
	public List<MerchantRatePercent> selectAllMerchantRatePercent() {
		return merchantRatePercentMapper.selectAll();
	}
}
